package state.font;

import particle.core.tools.Rect;
import particle.core.view.assets.Font;
import state.Clip;
import state.GameConstants;
import state.Graphics;
import state.Sprite;
import state.component.GameScrollBar;

public class ImageFontImpl implements ImageFont {
  private Font font = null;
  private Font boldFont = null;

  protected Font currentUseFont = null;
  protected Rect rect = new Rect();
  protected Clip reuseClip = new Clip(0, 0, 0, 0);

  public ImageFontImpl(Font font1, Font boldFont1) {
    this.font = font1;
    this.boldFont = boldFont1;
    currentUseFont = font1;
  }

  public int drawString(Graphics g, String text, int x, int y) {
    g.getSystemGraphics().setTextFont(currentUseFont);
    g.getSystemGraphics().drawText(text, x, y);
    return x + currentUseFont.getTextWidth(text);
  }

  public int drawString1(Graphics g, String text, int x, int y, int anchors) {
    int width = currentUseFont.getTextWidth(text);
    int height = currentUseFont.getLineHeight();
    int realX = Sprite.calculateX(x, anchors, width);
    int realY = Sprite.calculateY(y, anchors, height);
    g.getSystemGraphics().setTextFont(currentUseFont);
    rect.left = realX;
    rect.right = realX + width;
    rect.top = realY;
    rect.bottom = realY + height;
    g.getSystemGraphics().drawTextR(text, rect, GameConstants.TOP_LEFT_ANCHOR, false, false);
    return realX + width;
  }

  public int drawString2(Graphics g, String text, int color, int x, int y, int anchors) {
    g.setColor(color);
    return drawString1(g, text, x, y, anchors);
  }

  public int drawString3(Graphics g, String text, int color, int x, int y, int anchors, GameScrollBar scrollBar) {
    Clip.getClip1(g, reuseClip);
    scrollBar.getView().clipRect(g);
    int xx = drawString2(g, text, color, x, y, anchors);
    scrollBar.updateScroll(y, y + getHeight());
    reuseClip.applyClip(g);
    return xx;
  }

  public int drawSubstring(Graphics g, String text, int offset, int length, int x, int y) {
    return drawString(g, text.substring(offset, offset + length), x, y);
  }

  public int drawSubstring1(Graphics g, String text, int offset, int length, int x, int y, int anchors) {
    return drawString1(g, text.substring(offset, offset + length), x, y, anchors);
  }

  public int drawSubstring2(Graphics g, String text, int color, int offset, int length, int x, int y, int anchors) {
    return drawString2(g, text.substring(offset, offset + length), color, x, y, anchors);
  }

  public int drawSubstring3(Graphics g, String text, int color, int offset, int length, int x, int y, int anchors, GameScrollBar scrollBar) {
    return drawString3(g, text.substring(offset, offset + length), color, x, y, anchors, scrollBar);
  }

  public int stringWidth(String str) {
    return currentUseFont.getTextWidth(str);
  }

  public int substringWidth(String str, int offset, int length) {
    return stringWidth(str.substring(offset, offset + length));
  }

  public int charWidth(char c) {
    return stringWidth(String.valueOf(c));
  }

  public int drawOneChar1(Graphics g, char c, int x, int y) {
    return 0;
  }

  public int drawOneChar(Graphics g, char c, int color, int x, int y) {
    return 0;
  }

  public int getHeight() {
    return currentUseFont.getLineHeight();
  }

  public boolean isBold() {
    return currentUseFont == boldFont;
  }

  public void setBold(boolean isBold) {
    currentUseFont = isBold ? boldFont : font;
  }

  public Font getSystemFont() {
    return currentUseFont;
  }

  public void detroy() {
    font = null;
    boldFont = null;
    currentUseFont = null;
    rect = null;
    reuseClip = null;
  }
}
